/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

import javax.swing.InputMap;

/**
 * 	Controla o Socket Servidor
 * @author matheus
 */
public class ControllerServer {

	private ServerSocket sock;
	private Socket client;
	private PrintWriter pout; //Escritor
	//private Scanner in;
	private BufferedReader in; //Leitor
	public ControllerServer(int porta){
		try{
			sock = new ServerSocket(porta);
			client = sock.accept(); //Fica esperando ate obtera conexao
			//Monta os campos
			pout  = new PrintWriter(client.getOutputStream(), true);
			in = new BufferedReader(new InputStreamReader(client.getInputStream()));
		}catch (Exception e) {
			e.printStackTrace();
			System.out.println("Problemas ao criar os servidor");
		}
	}

	public int readInt(){
		System.out.println("Servidor Lendo");
		String temp;
    	try{
    		
    		temp = in.readLine();	
    		System.out.println(temp);
    	}catch (IOException e) {
    		return -1;
    	}
    	return Integer.parseInt(temp);
	}    

	public void writeInt(int i){
		System.out.println("Servidor " + i);
		pout.println(i);

	}

	public void enviaString(String s){
        pout.println(s);    
    }
	public String leString(){
		try {
			return in.readLine();
		} catch (IOException e) {
			return "";
		}
	}

}
